package cn.xz.kjg.activity


import android.graphics.drawable.Drawable
import android.os.Handler
import android.util.Log
import android.widget.SeekBar
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import cn.xz.kjg.R
import cn.xz.kjg.adapter.*
import cn.xz.kjg.base.BaseActivity
import cn.xz.kjg.base.BaseApplication
import cn.xz.kjg.bean.CommonLib
import cn.xz.kjg.bean.DeviceList
import cn.xz.kjg.bean.HP
import cn.xz.kjg.bean.U3dListBean
import cn.xz.kjg.databinding.ActivitySecondBinding
import cn.xz.kjg.dialog.LocalListDialog
import cn.xz.kjg.net.ControlDevice
import cn.xz.kjg.net.OkHttpUtil
import cn.xz.kjg.util.*

class SecondActivity : BaseActivity<ActivitySecondBinding>(R.layout.activity_second) {

    //标识符
    private var mTag = 0
    //设备信息
    private var mDevice: DeviceList? = null
    //当前视频进度值
    private var pMove = 0
    private var pMove1 = 0
    private var pMove4 = 0
    private var handler: Handler? = null

    private var mAdapter1: SPAdapter? = null
    private var mAdapter2: SPAdapter? = null
    private var mAdapter3: SPAdapter? = null
    private var mAdapter4: SPAdapter? = null
    private var mU3dAdapter: U3dAdapter? = null
    private var mTmAdapter: U3dAdapter? = null

    //宣传片--合
    var m0 = -1

    override fun initData() {
        //获取跳转数据
        mTag = intent.getIntExtra(CommonLib.JUMP_TYPE, -1)
        //获取当前设备信息
        if (mTag != -1){
            mDevice = ControlDevice.getThisDevice(mTag)
        }
        //如果数据异常直接退出
        if (mTag == -1 || mDevice == null){
            ToastUtil.show("当前数据异常")
            finish()
        }else{
            initTag(mTag)
        }
        //获取声音和视频进度
        initHD()
        //初始化List
        initRv()
    }

    private fun initHD(){
        //获取当前音量
        getVoice()
        //开启定时器获取视频进度
        handler = Handler()
        handler?.post(movieR)
    }

    private fun getVoice(){
        LocalHttp.getVoiceSize(mDevice!!.ip, LocalHttp.GetVoiceSizeListener {
            mDataBind.zkVolumeSeekbar.post {
                mDataBind.zkVolumeSeekbar.progress = it
                mDataBind.zkVolumeSeekbar1.progress = it
                mDataBind.zkVolumeSeekbar4.progress = it
            }
        })
    }

    //获取本地资源列表
    private fun getLocalList(i: Int){
        LocalHttp.getPlayData(this,mDevice!!.ip,LocalHttp.GetPlayDataListener {
            if (it.isNotEmpty()){
                if (mDevice != null){
                    if (i == 14000){
                        //活动
                        mDataBind.rvLList.layoutManager = LinearLayoutManager(BaseApplication.getContext())
                        mDataBind.rvLList.itemAnimator = DefaultItemAnimator()
                        val mAdapter = LocalListAdapter1(mDevice!!)
                        mDataBind.rvLList.adapter = mAdapter
                        mAdapter.setList(it)
                    }
                    if (i == 14004){
                        val mL: ArrayList<HP> = arrayListOf()
                        it.forEach {
                            mL.add(HP(it.id,it.title))
                        }
                        //透明屏
                        if (mTmAdapter != null){
                            mTmAdapter!!.setList(mL)
                        }
                    }
                }
            }
        })
    }

    private fun initTag(i: Int){
        mDevice = ControlDevice.getThisDevice(i)

        mDataBind.tvSyy.gone()
        mDataBind.tvXyy.gone()

        var mH: Drawable? = null
        var mL: Drawable? = null
        var mT: Drawable? = null
         when(i){
             14000->{
                 mDataBind.clHd.visible()
                 mDataBind.clLk.gone()
                 mDataBind.clTm.gone()

                 mDataBind.tvSyy.visible()
                 mDataBind.tvXyy.visible()

                 mH = AppHelper.getDrawable(R.mipmap.icon_sss)
                 shoHys(true)
                 //活动相关
                 getLocalList(i)
             }
             14001->{
                 mDataBind.clHd.gone()
                 mDataBind.clLk.visible()
                 mDataBind.clTm.gone()
                 mL = AppHelper.getDrawable(R.mipmap.icon_sss)
                 //获取临空优势数据
                 initLK()
                 shoHys(false)
             }
             14004->{
                 mDataBind.clHd.gone()
                 mDataBind.clLk.gone()
                 mDataBind.clTm.visible()
                 mT = AppHelper.getDrawable(R.mipmap.icon_sss)

                 //透明屏相关
                 getLocalList(i)
                 shoHys(false)
             }
         }
        mDataBind.tvHd.background = mH
        mDataBind.tvLk.background = mL
        mDataBind.tvTm.background = mT
    }

    override fun initEvent() {
        //返回
        mDataBind.tvBack.setOnClickListener {
            finish()
        }
        mDataBind.llcHd.setOnClickListener {
            initTag(14000)
        }
        mDataBind.llcLk.setOnClickListener {
            initTag(14001)
        }
        mDataBind.llcTm.setOnClickListener {
            initTag(14004)
        }
        //播放
        mDataBind.tvPlay.setOnClickListener {
            setPlay(1)
        }
        //暂停
        mDataBind.tvT.setOnClickListener {
            setPlay(2)
        }
        //重播
        mDataBind.tvR.setOnClickListener {
            setPlay(3)
        }
        //鼠键
        mDataBind.tvX.setOnClickListener {
            ARouter.to(SJActivity::class.java,mDevice!!.ip)
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                pMove = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                LocalHttp.changeMovie(mDevice!!.ip,pMove)
            }
        })
        //合屏右
        mDataBind.tvB1.setOnClickListener {
            showP(0)
        }
        //合屏中
        mDataBind.tvB2.setOnClickListener {
            showP(1)
        }
        //分屏
        mDataBind.tvB3.setOnClickListener {
            showP(2)
        }
        //U3d播放
        mDataBind.tvPlay3.setOnClickListener {
            U3dHttp.PlayMovie(mDevice!!.ip)
        }
        //U3d暂停
        mDataBind.tvT3.setOnClickListener {
            U3dHttp.PauseMovie(mDevice!!.ip)
        }
        //U3d重播
        mDataBind.tvR3.setOnClickListener {
            U3dHttp.ReplayMovie(mDevice!!.ip)
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar1.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar1.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                pMove1 = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                U3dHttp.changeMovie(mDevice!!.ip,pMove1)
            }
        })
        //透明屏
        //客户端播放
        mDataBind.tvPlay4.setOnClickListener {
            setPlay(1)
        }
        //客户端暂停
        mDataBind.tvT4.setOnClickListener {
            setPlay(2)
        }
        //客户端重播
        mDataBind.tvR4.setOnClickListener {
            setPlay(3)
        }
        //声音滑动监听
        mDataBind.zkVolumeSeekbar4.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                SeekBarPopUtils.move(progress, seekBar, false)
                LocalHttp.changeVoice(mDevice!!.ip,progress)
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.showPop(seekBar)
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                SeekBarPopUtils.dismiss()
            }
        })
        //视频滑动监听
        mDataBind.zkVideoSeekbar4.setOnSeekBarChangeListener(object :
            SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                pMove4 = progress
            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                LocalHttp.changeMovie(mDevice!!.ip,pMove4)
            }
        })
        //投屏信号
        mDataBind.tvHysTp.setOnClickListener {
            val code = "" +
                    "55 aa 00 3e fe" +
                    "00 00 00 00 00" +
                    "01 00 12 00 02" +
                    "13 03 00 00 00" +
                    "90 4c 57"
            val packet = byteArrayOf(
                  0x55.toByte(), 0xaa.toByte(), 0x00.toByte(), 0x3e.toByte(), 0xfe.toByte()
                , 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte()
                , 0x01.toByte(), 0x00.toByte(), 0x12.toByte(), 0x00.toByte(), 0x02.toByte()
                , 0x13.toByte(), 0x03.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte()
                , 0x90.toByte(), 0x4c.toByte(), 0x57.toByte()
            )
            SendTcpUtils.setTcp(packet,"192.168.10.10",5200)
        }
        //主机信号
        mDataBind.tvHysZj.setOnClickListener {
            val code = "" +
                    "55 aa 00 b4 fe" +
                    "00 00 00 00 00" +
                    "01 00 12 00 02" +
                    "13 03 00 01 00" +
                    "80 b3 57"
            val packet = byteArrayOf(
                  0x55.toByte(), 0xaa.toByte(), 0x00.toByte(), 0xb4.toByte(), 0xfe.toByte()
                , 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte(), 0x00.toByte()
                , 0x01.toByte(), 0x00.toByte(), 0x12.toByte(), 0x00.toByte(), 0x02.toByte()
                , 0x13.toByte(), 0x03.toByte(), 0x00.toByte(), 0x01.toByte(), 0x00.toByte()
                , 0x80.toByte(), 0xb3.toByte(), 0x57.toByte()
            )
            SendTcpUtils.setTcp(packet,"192.168.10.10",5200)
        }
        mDataBind.tvXyy.setOnClickListener {
            LocalHttp.moviePlay(mDevice!!.ip,8)
        }
        mDataBind.tvSyy.setOnClickListener {
            LocalHttp.moviePlay(mDevice!!.ip,7)
        }
    }

    private fun shoHys(b: Boolean){
        if (b){
            mDataBind.tvHysTp.visible()
            mDataBind.tvHysZj.visible()
        }else{
            mDataBind.tvHysTp.gone()
            mDataBind.tvHysZj.gone()
        }
    }


    private fun showP(i: Int){
        when(i){
            0->{
                mDataBind.clH.visible()
                mDataBind.clF.gone()
            }
            1->{
                OkHttpUtil.post("http://${mDevice!!.ip}:8030/?act=unity&states=MergeScreen&object=")
                U3dHttp.PostChannel(mDevice!!.ip,m0)
                mDataBind.clH.visible()
                mDataBind.clF.gone()
            }
            2->{
                //分
                OkHttpUtil.post("http://${mDevice!!.ip}:8030/?act=unity&states=SplitScreen&object=")
                mDataBind.clH.gone()
                mDataBind.clF.visible()
            }
        }
    }

    private var movieR: Runnable = object : Runnable {
        override fun run() {
            getMove()
            handler?.postDelayed(this, 1000)
        }
    }

    private fun getMove(){
        LocalHttp.getMovieSize(mDevice!!.ip,LocalHttp.GetMovieSizeListener { maxTxt, minTxt, maxSeekBar, seekProgress ->
            mDataBind.zkVideoSeekbar.post {
                mDataBind.zkMaxTime.text = maxTxt
                mDataBind.zkMinTime.text = minTxt
                mDataBind.zkVideoSeekbar.max = maxSeekBar
                mDataBind.zkVideoSeekbar.progress = seekProgress
            }

            mDataBind.zkVideoSeekbar4.post {
                mDataBind.zkMaxTime4.text = maxTxt
                mDataBind.zkMinTime4.text = minTxt
                mDataBind.zkVideoSeekbar4.max = maxSeekBar
                mDataBind.zkVideoSeekbar4.progress = seekProgress
            }
        })
        U3dHttp.getMovieSize(mDevice!!.ip,object : U3dHttp.GetMovieSizeListener{
            override fun getData(
                maxTxt: String?,
                minTxt: String?,
                maxSeekBar: Int,
                seekProgress: Int
            ) {
                mDataBind.zkVideoSeekbar1.post {
                    mDataBind.zkMaxTime1.text = maxTxt
                    mDataBind.zkMinTime1.text = minTxt
                    mDataBind.zkVideoSeekbar1.max = maxSeekBar
                    mDataBind.zkVideoSeekbar1.progress = seekProgress
                }
            }

        })
    }


    private fun setPlay(i :Int){
        LocalHttp.moviePlay(mDevice!!.ip,i)
    }

    private fun initRv(){
        mAdapter1 = SPAdapter()
        mDataBind.rv1.layoutManager = LinearLayoutManager(this)
        mDataBind.rv1.adapter = mAdapter1
        mAdapter1!!.setOnItemClickListener { adapter, view, position ->
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            U3dHttp.PostContent(mDevice!!.ip,mData[position].id!!)
            adapter.notifyDataSetChanged()
        }

        mAdapter2 = SPAdapter()
        mDataBind.rv2.layoutManager = LinearLayoutManager(this)
        mDataBind.rv2.adapter = mAdapter2
        mAdapter2!!.setOnItemClickListener { adapter, view, position ->
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            U3dHttp.PostContent(mDevice!!.ip,mData[position].id!!)
            adapter.notifyDataSetChanged()
        }

        mAdapter3 = SPAdapter()
        mDataBind.rv3.layoutManager = LinearLayoutManager(this)
        mDataBind.rv3.adapter = mAdapter3
        mAdapter3!!.setOnItemClickListener { adapter, view, position ->
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            U3dHttp.PostContent(mDevice!!.ip,mData[position].id!!)
            adapter.notifyDataSetChanged()
        }

        mAdapter4 = SPAdapter()
        mDataBind.rv4.layoutManager = LinearLayoutManager(this)
        mDataBind.rv4.adapter = mAdapter4
        mAdapter4!!.setOnItemClickListener { adapter, view, position ->
            Log.e("mAdapter4", "initRv:12323123232132 ", )
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            U3dHttp.PostContent(mDevice!!.ip,mData[position].id!!)
            adapter.notifyDataSetChanged()
        }

        mU3dAdapter = U3dAdapter()
        mDataBind.rvHL.layoutManager = LinearLayoutManager(this)
        mDataBind.rvHL.adapter = mU3dAdapter
        mU3dAdapter!!.setOnItemClickListener { adapter, view, position ->
            Log.e("mAdapter4", "initRv:12323123232132 ", )
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            U3dHttp.PostChannel(mDevice!!.ip,mData[position].id!!)
            adapter.notifyDataSetChanged()
        }


        mTmAdapter = U3dAdapter()
        mDataBind.rvTm.layoutManager = LinearLayoutManager(this)
        mDataBind.rvTm.adapter = mTmAdapter
        mTmAdapter!!.setOnItemClickListener { adapter, view, position ->
            Log.e("mAdapter4", "initRv:12323123232132 ", )
            val mData = adapter.data as ArrayList<HP>
            mData.forEach {
                it.select = false
            }
            mData[position].select = true
            LocalHttp.playLocal(mDevice!!.ip,position)
            adapter.notifyDataSetChanged()
        }
    }


    private fun initLK(){
        U3dHttp.getU3dData(this,mDevice!!.ip,object :U3dHttp.GetU3dDataListener{
            override fun getData(mBean: U3dListBean?) {
                 if(mBean != null){
                     //标题
                     val mSub = mBean.subColumns
                     //交通优势
                     var m1 = -1
                     //产业优势
                     var m2 = -1
                     //发展机遇
                     var m3 = -1
                     //发展支撑
                     var m4 = -1
                     mSub.forEach {
                         var nName = it.name
                         if (nName.contains("交通优势")){
                             m1 = it.tag
                         }else if (nName.contains("产业优势")){
                             m2 = it.tag
                         }else if (nName.contains("发展机遇")){
                             m3 = it.tag
                         }else if (nName.contains("发展支撑")){
                             m4 = it.tag
                         }else if (nName.contains("宣传")){
                             m0 = it.tag
                         }
                     }

                     val mC = mBean.list

                     //合屏
                     val mL0: ArrayList<HP> = arrayListOf()
                     //交通优势
                     val mL1: ArrayList<HP> = arrayListOf()
                     //产业优势
                     val mL2: ArrayList<HP> = arrayListOf()
                     //发展机遇
                     val mL3: ArrayList<HP> = arrayListOf()
                     //发展支持
                     val mL4: ArrayList<HP> = arrayListOf()
                     mC.forEach {
                         if (it.tag == m0){
                             mL0.add(HP(it.id,it.title))
                         }
                         if (it.tag == m1){
                             mL1.add(HP(it.id,it.title))
                         }
                         if (it.tag == m2){
                             mL2.add(HP(it.id,it.title))
                         }
                         if (it.tag == m3){
                             mL3.add(HP(it.id,it.title))
                         }
                         if (it.tag == m4){
                             mL4.add(HP(it.id,it.title))
                         }
                     }
                     if (mU3dAdapter != null){
                         mU3dAdapter!!.setList(mL0)
                     }
                     if (mAdapter1 != null){
                         mAdapter1!!.setList(mL1)
                     }
                     if (mAdapter2 != null){
                         mAdapter2!!.setList(mL2)
                     }
                     if (mAdapter3 != null){
                         mAdapter3!!.setList(mL3)
                     }
                     if (mAdapter4 != null){
                         mAdapter4!!.setList(mL4)
                     }
                 }
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        if (handler != null){
            handler?.removeCallbacks(movieR)
            handler = null
        }
    }
}